package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class DelMRitem
 */ 
public class ManipulateGridSquad extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ManipulateGridSquad() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doPost(request,response);
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		
		Widgets wid = new Widgets();
		Connection conn = wid.getConn(true);
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String strSQL = "";
		String oper = request.getParameter("oper");						//增刪改查那一種
		String bssId = "MBMS";
		String squadId = request.getParameter("squadid");
		String subSysId = request.getParameter("subSysId");
		String envId = request.getParameter("envId");
		String path = request.getParameter("path");						//部署班的部署路徑
		String result = "";
		String currentUser = "842297";
		
		/** 判別需進行何種操作 **/
		if ( "del".equalsIgnoreCase(oper) ) {
			strSQL = "DELETE FROM squad WHERE squadid=? AND bssid=? AND subsysid=? AND envid=?";
			squadId = request.getParameter("id");
			try {
				ps = conn.prepareStatement(strSQL);
				ps.setString(1, squadId);
				ps.setString(2, bssId);
				ps.setString(3, subSysId);
				ps.setString(4, envId);
				ps.execute();
				
				wid.keepLogProfile(wid.SQUAD, wid.DELETE, currentUser, conn, ps);
				result = "刪除了"+squadId;
			} catch (SQLException e) {
				result = "刪除"+squadId+"失敗!!\n\n";
				result += wid.errProcess(e);
			}
		} else if ( "add".equalsIgnoreCase(oper) ) {
			try {
				if (!isDuplicated(squadId,bssId,subSysId,envId,conn,ps,rs)) {
					strSQL = "INSERT INTO squad (subsysid,squadname,creator,createdDateTime,bssid,squadid,envid,path) VALUES (?,?,?,?,?,?,?,?)";
					String squadName = request.getParameter("squadname");
					ps = conn.prepareStatement(strSQL);
					ps.setString(1, subSysId);
					ps.setString(2, squadName);
					ps.setString(3, currentUser);
					ps.setTimestamp(4, wid.currentTimeStamp());
					ps.setString(5, bssId);
					ps.setString(6, squadId);
					ps.setString(7, envId);
					ps.setString(8, path);
					ps.execute();
					
					wid.keepLogProfile(wid.SQUAD, wid.ADD, currentUser, conn, ps);
					result = "新增完成 "+squadId;
				} else {
					result = squadId+" 已存在 !";		
				}
			} catch (SQLException e) {
				result = "新增"+squadId+"失敗!!\n\n";
				result += wid.errProcess(e);
			}
		} else if ( "edit".equalsIgnoreCase(oper) ) {
			squadId = request.getParameter("id");
			String squadName = request.getParameter("squadname");
			strSQL = "UPDATE squad SET squadid=?,squadname=?,path=?,lastUpdator=?,lastUpdatedDateTime=? WHERE subsysid=? AND bssid=? AND squadid=? AND envid=?";
			try {
				ps = conn.prepareStatement(strSQL);
				ps.setString(1, squadId);
				ps.setString(2, squadName);
				ps.setString(3, path);
				ps.setString(4, currentUser);
				ps.setTimestamp(5, wid.currentTimeStamp());
				ps.setString(6, subSysId);
				ps.setString(7, bssId);
				ps.setString(8, squadId);
				ps.setString(9, envId);
				ps.execute();
				
				wid.keepLogProfile(wid.SQUAD, wid.UPDATE, currentUser, conn, ps);
				result = "修改完成 "+squadId;

			} catch (SQLException e) {
				result = "修改"+squadId+"失敗!!\n\n";
				result += wid.errProcess(e);
			}
		}

		try {
			if (rs!= null) { rs.close(); }
			if (ps!= null) { ps.close(); }
			if (conn!= null) {conn.close();}
		} catch (SQLException e) {
			System.out.println(wid.errProcess(e));
		}
		
		response.setContentType("application/text");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(result);		
	}
	
	private boolean isDuplicated(String squadId,String bssId,String subSysId,String envId,Connection conn,PreparedStatement ps, ResultSet rs) throws SQLException {
		String strSQL = "SELECT COUNT(*) FROM squad WHERE squadid=? AND bssid=? AND subsysid=?  AND envid=?";
		
		ps = conn.prepareStatement(strSQL);
		ps.setString(1, squadId);
		ps.setString(2, bssId);
		ps.setString(3, subSysId);
		ps.setString(4, envId);
		rs = ps.executeQuery();
		rs.next();

		return rs.getInt(1) == 0?false:true;
	}
	
}